/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azhar.com;

import azhar.view.Contacts;
import azhar.view.Main;
import azhar.view.control.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 *
 * @author Al-Insan
 */
public class PopupMenuContact extends JPopupMenu {

    private JMenuItem SEND_MESSAGE = new JMenuItem("Send Message");
    private JMenuItem DELETE_CONTACT = new JMenuItem("Delete Contact");
    private JMenuItem EDIT_CONTACT = new JMenuItem("Edit Contact");
    private MouseAdapter[] mouse = new MouseAdapter[3];
    private TableOperation tabOp;
    private Main main;
    private Contacts contact;
    public JTable table;

    public PopupMenuContact(JTable t, Main m, Contacts c) {
        tabOp = new TableOperation();
        table = t;
        main = m;
        contact = c;
        setMenu();
    }

    private void createMenu() {
        add(SEND_MESSAGE);
        add(new JSeparator(JSeparator.HORIZONTAL));
        add(EDIT_CONTACT);
        add(DELETE_CONTACT);
    }

    private void setEnableAll() {
        setAllAction();
        SEND_MESSAGE.setEnabled(true);
        if (table.getSelectedRowCount() < 2) {
            EDIT_CONTACT.setEnabled(true);
        } else {
            EDIT_CONTACT.setEnabled(false);
            EDIT_CONTACT.removeMouseListener(mouse[2]);
        }
        DELETE_CONTACT.setEnabled(true);
    }

    private void setAllAction() {
        mouse[0] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                String numbers = new Separate().separated(tabOp.getValues(table, "No HP"));
                new CreateMessage(main, true, numbers).setVisible(true);
            }
        };
        mouse[1] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                for (int i = table.getSelectedRows().length-1; i >=0 ; i--) {
                    contact.deleteData(table.getAccessibleContext().getAccessibleName(), table.getSelectedRows()[i]);
                }
                tabOp.deleteData(table, table.getSelectedRows());
            }
        };
        mouse[2] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                int idx = table.getSelectedRow();
                String name = table.getAccessibleContext().getAccessibleName();
                goToUpdate(name, idx);
            }
        };
        SEND_MESSAGE.addMouseListener(mouse[0]);
        DELETE_CONTACT.addMouseListener(mouse[1]);
        EDIT_CONTACT.addMouseListener(mouse[2]);
    }

    public void setMenu() {
        createMenu();
        setEnableAll();
    }

    public int getColumnIndex(String name) {
        int index = 0;
        for (int i = 0; i < table.getColumnCount(); i++) {
            index = i;
            if (table.getColumnName(i).equals(name)) {
                break;
            }
        }
        return index;
    }

    public void goToUpdate(String n, int idx) {
        if (n.equals("guru")) {
            new EditGuru(main, true, contact, table, idx).setVisible(true);
        } else if (n.equals("karyawan")) {
            new EditKaryawan(main, true, contact, table, idx).setVisible(true);
        } else if (n.equals("siswa")) {
            new EditSiswa(main, true, contact, table, idx).setVisible(true);
        } 
    }
}
